package com.wms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wms.bean.PageBean;
import com.wms.entity.Category;
import com.wms.entity.Material;
import com.wms.entity.Stock;
import com.wms.mapper.CategoryMapper1;
import com.wms.mapper.MaterialMapper;
import com.wms.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.DispatcherServlet;

import java.util.List;

@Service
public class MaterialServiceImpl implements MaterialService {
    @Autowired
    MaterialMapper materialMapper;
    @Autowired
    CategoryMapper1 categoryMapper1;

    @Override
    public PageBean getMaterial(int page,int pageSize,Material material) {
        PageHelper.startPage(page,pageSize);
        List<Material> list=materialMapper.getMaterial(material);
        PageInfo info=new PageInfo(list);
        return new PageBean<>(page,pageSize,info.getTotal(),list);
    }

    @Override
    @Transactional
    public int addMaterial(Material material) {
        String code=getMaterialCode(material.getCategoryCode());
        material.setCode(code);
        return materialMapper.addMaterial(material);
    }

    /**
     * 自动获取物料编码
     * @param
     * @return
     */
    private String getMaterialCode(String categoryCode){
        String newCode=null;
        String maxCode=materialMapper.queryMaxMaterialCode(categoryCode);
        if (null==maxCode){
            //判断是否存在此类别编码
          List<Category> list= categoryMapper1.getCategory();
          boolean isExist=false;
            for (Category c : list) {
                //如果存在但还没有该类别物料,直接赋初始值
                if (c.getCode().equals(categoryCode)) {
                    newCode = "WL"+categoryCode + "001";
                    isExist=true;
                }
            }
            if (!isExist) throw new RuntimeException("该物料类别不存在,请先创建");
        }else {
            //该类别下已经存在物料,直接添加
            long tmp=Long.parseLong(maxCode);
            tmp++;
            newCode=String.valueOf(tmp);
        }
        return newCode;
    }

    @Override
    @Transactional
    public int updateMaterial(Material material) {
        return materialMapper.updateMaterial(material);
    }
    @Transactional
    public int deleteMaterial(int id){return materialMapper.deleteMaterial(id);}

    @Override
    @Transactional
    public int setStock(Stock stock) {
        //查询该物料是否存在库存信息
        int rs;
       if (null!=materialMapper.queryStock(stock.getId())){
            //存在库存信息,直接修改
            rs=materialMapper.updateStock(stock);
            materialMapper.updatesetStatus(stock.getId());
       }else {
           //不存在该物料库存信息,直接插入
            rs=materialMapper.insertStock(stock);
            materialMapper.updatesetStatus(stock.getId());
       }
        return rs;
    }
}
